package com.freshcity.page.wait
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.OpCodes;
	import com.freshcity.graphics.LineListWin;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	
	public class CityList extends Component
	{
		public var owner:RoomList;
		private var _lineList:Array=[];
		private var background:LineListWin;
		
		public function CityList(parent:DisplayObjectContainer,xpos:Number=0,ypos:Number=0)
		{
			this.mouseEnabled = false;
			super(parent,xpos,ypos);
		}		
		
		private function onLoad(data:*):void
		{
			var len:int = data.length;
			var startX:int = 20;
			var startY:int = 20;
			
			var gridWidth:int = 60;
			var gridHeight:int = 30;
			
			var mainSprite:Sprite = new Sprite();
			
			for (var i:int = 0; i < len; i++) 
			{
				var thing:TextField = StringUtil.addHyLink(data[i].name,data[i].station,onViewCity);
				thing.x = startX + int(i%4)* gridWidth;
				thing.y = startY + int(i/4) * gridHeight;
				mainSprite.addChild(thing);				
			}
			this.background.panel.source=mainSprite;
		}
		
		private function onViewCity(event:TextEvent):void
		{
			owner.group.commu.send(OpCodes.VISIT_CITY,{sid:event.text},owner.show);
			TweenLite.to(this,0.5,{x:390});
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			background = new LineListWin();
			addChild(background);
			background.closeBtn.addEventListener(MouseEvent.CLICK,onCloseWinHandler);
		}
		
		override public function draw():void
		{
			super.draw();
			owner.group.commu.send(OpCodes.CITY_LIST,{}, onLoad);
		}
		
		protected function onCloseWinHandler(event:MouseEvent):void
		{
			TweenLite.to(this,0.5,{x:390});
		}

	}
}
